# 数据存储查询
import os
from config.seting import BASE_DB
import json


def search(username):
    user_file = os.path.join(BASE_DB, "%s.json" % (username))
    if os.path.exists(user_file):
        with open(user_file, "r", encoding="UTF-8") as f:
            data = json.load(f)
            return data
    else:
        return False


def save(user_info):
    user_file = os.path.join(BASE_DB, "%s.json" % (user_info["username"]))
    with open(user_file, "w", encoding="UTF-8") as f:
        json.dump(user_info, f)
        f.flush()


def select_comm():
    user_file = os.path.join(BASE_DB, "shopping.json")
    with open(user_file, "r", encoding="UTF-8") as f:
        data = json.load(f)
    return data


def save_shoping(shopping_info):
    shopping_file = os.path.join(BASE_DB, "%sshoping.json" % (shopping_info["username"]))
    with open(shopping_file, "w", encoding="UTF-8") as f:
        json.dump(shopping_info, f)
        f.flush()


def select_shopping(username):
    user_file = os.path.join(BASE_DB, "%sshoping.json" %username)
    with open(user_file, "r", encoding="UTF-8") as f:
        data = json.load(f)
    return data
